package com.situ.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import com.situ.service.FOODService;
import com.situ.entity.FOOD;
import com.situ.test.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.nio.file.Path;
import java.sql.Date;
import java.util.List;

/**
 * FOOD的路由接口服务
 * 
 * @author胡文
 *
 */
//
@Api(tags = "权限菜单管理")
@RestController
public class FOODController {

	/** FOODService服务 */
	@Autowired
	private FOODService fOODService;
	
	/**
	 * 查询所有FOOD数据的方法
	 * @param value
	 * @return
	 */
	@GetMapping(value = "/FOOD/query", produces = {"application/json;charset=UTF-8"})
	public ResponseData query(FOOD value) {

		return ResponseData.ok().putDataValue("list",fOODService.find(value));
	}
	
	/**
	 * 通过id查询FOOD数据的方法
	 * 
	 * @return
	 */
	@RequestMapping(value = "/FOOD/queryById/{id}", produces = {"application/json;charset=UTF-8"})
	public ResponseData queryById(@PathVariable(name="id") Integer id) {
        System.out.println("我进来了");
		 return ResponseData.ok().putDataValue("obj",fOODService.findOne(id));
	}
	
	/**
	 * 插入FOOD属性不为空的数据方法
	 * 
	 * @return
	 */
	@RequestMapping(value = "/FOOD/save")
	public ResponseData save(FOOD value) {

        return ResponseData.ok().putDataValue("obj", fOODService.saveNotNull(value));
	}
	
	/**
	 * 更新FOOD属性不为空的数据方法
	 * 
	 * @return
	 */
	@PutMapping(value = "/FOOD/update", produces = {"application/json;charset=UTF-8"})
	public ResponseData update(FOOD value,int fid) {
        System.out.println("fid===="+fid);
	    String msg="修改成功";
        System.out.println("update=========="+value);
        int i=0;
        try {
            i=fOODService.updateNotNullById(value);
        } catch (Exception e) {
            msg="修改异常";
            e.printStackTrace();
        }
        value.setFdate(new Date(System.currentTimeMillis()));
		return ResponseData.ok().putDataValue("obj",i)
                .putDataValue("msg", msg);
	}

	/**
	 * 通过id删除FOOD数据方法
	 * 
	 * @return
	 */
    @ApiOperation("删除根据id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "传入权限id",value = "范围1-x" ,required = false)
    })
	@RequestMapping(value = "/FOOD/delById/{id}", produces = {"application/json;charset=UTF-8"})
	public ResponseData delById(@PathVariable(name="id") Integer id) {
		return ResponseData.ok().putDataValue("obj",fOODService.deleteById(id));
	}




    //带分页的查询全部
//@ApiOperation("带分页的查询全部")

//    @RequestMapping(value = "/Food/queryPage", produces = {"application/json;charset=UTF-8"})
//    public ResponseData queryPage(FOOD value,
//                                  @RequestParam(defaultValue = "1",required = false) int pageNum ,
//                                  @RequestParam(defaultValue = "3",required = false) int pageSize) {
//
//
//        PageHelper.startPage(pageNum, pageSize);
//        List<FOOD> list = fOODService.find(value);
//
//        PageInfo pageInfo = new PageInfo<FOOD>(list,5);
//
//        return ResponseData.ok()
//                // .putDataValue("list", list)
//                .putDataValue("pageInfo",pageInfo);
//    }

    @RequestMapping(value = "/Food/queryPage", produces = {"application/json;charset=UTF-8"})
    public ResponseData queryPage(FOOD value,
                                  @RequestParam(defaultValue = "1",required = false) int pageNum ,
                                  @RequestParam(defaultValue = "3",required = false) int pageSize) {

        PageInfo pageInfo = fOODService.find2(value, pageNum, pageSize);

        return ResponseData.ok()
                // .putDataValue("list", list)
                .putDataValue("pageInfo",pageInfo);
    }

    @Value("${path1}")
    private String path;

    @RequestMapping(value = "/Food/uploadfood")
    public ResponseData uploadfood(MultipartFile file, FOOD value) {

        try {

            //获得读文件的流
            InputStream is = file.getInputStream();

            //当前时间毫秒 +扩展名
            String filename= System.currentTimeMillis()+
                    file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));

            File dir = new File(path+"/images/");
            if(!dir.exists()){
                dir.mkdirs();// 不存在就创建文件夹
            }
            System.out.println("page~~~~:"+dir.getAbsolutePath());

            //写入文件的位置和名字
            File f1 = new File(dir, filename);


            FileOutputStream fos=new FileOutputStream(f1);

            byte [] b=new byte[1024*50];

            int len=0;
            while((len=is.read(b))>0){
                fos.write(b,0,len);
            }
            fos.close();
            is.close();
            value.setImage(filename);//把文件名保存到对象


        } catch (Exception e) {
            e.printStackTrace();
        }


        return ResponseData.ok().putDataValue("obj", fOODService.saveNotNull(value));
    }


    //带分页的查询全部
//@ApiOperation("带分页的查询全部")
/*
    @RequestMapping(value = "/FOOD/queryPage", produces = {"application/json;charset=UTF-8"})
    public ResponseData queryPage(FOOD value,
                             @RequestParam(defaultValue = "1",required = false) int pageNum ,
                             @RequestParam(defaultValue = "5",required = false) int pageSize) {

	    PageHelper.startPage(pageNum, pageSize);
        List<SysMight> list = sysMightService.find2(value);
    PageInfo pageInfo = new PageInfo<SysMight>(list,5);

        return ResponseData.ok()
        // .putDataValue("list", list)
        .putDataValue("pageInfo",pageInfo);
        }
 */
}
